package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Random;

/**
 * @version 1.0
 * @auther Administrator
 * @date 2023/2/22 15:04
 */
@Service
public class DtsStorageServiceImpl implements DtsStorageService {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint; //类的成员变量
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;
    @Autowired
    private DtsStorageMapper dtsStorageMapper;
    
    /**
     * 上传图片
     * @param ext
     * @param size
     * @param bytes
     * @return
     */
    @Override
    public DtsStorage create(String ext, int size, byte[] bytes, String filename) throws Exception{
        DtsStorage dtsStorage = new DtsStorage();
        dtsStorage.setType(ext);
        //时间戳 UUID
        String objectName = System.currentTimeMillis() + new Random().nextInt(100000) + "." + ext;
        //1:上传文件到OOS
        String url = AliyunOSSUtil.uploadImage(
                endpoint,
                accessKeyId,
                accessKeySecret,
                bucketName,
                objectName,
                bytes
        );
        dtsStorage.setKey(objectName);
        dtsStorage.setUrl(url);
        dtsStorage.setName(filename);
        dtsStorage.setSize(size);
        dtsStorage.setType("." + ext);

        dtsStorageMapper.insert(dtsStorage);

        //TODO真正写完 需要保存到数据库
        return dtsStorage;
    }

    /**
     * 查看
     * @param key
     * @param page
     * @param limit
     * @return
     */
    @Override
    public IPage<DtsStorage> findListByQuery(String key, String name, String sort, String order, Integer page, Integer limit) {
        return dtsStorageMapper.selectPage(new Page<DtsStorage>(page, limit)
                , new QueryWrapper<DtsStorage>()
                        .eq(!StringUtils.isEmpty(key), "key", key)
                        .like(!StringUtils.isEmpty(name),"name",name)
                        .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort)
        );
    }


    /**
     * 更新
     * @param dtsStorage
     * @return
     */
    @Override
    public DtsStorage update(DtsStorage dtsStorage) {
        dtsStorageMapper.updateById(dtsStorage);
        return dtsStorage;
    }

    /**
     * 删除
     * @param dtsStorage
     * @return
     */
    @Override
    public DtsStorage delete(DtsStorage dtsStorage) {
        String name = dtsStorage.getName();
        AliyunOSSUtil.deleteFile(
                endpoint,
                accessKeyId,
                accessKeySecret,
                bucketName,
                name
        );
        return dtsStorage;
    }



}
